package components
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Button;
	import mx.controls.ComboBox;
	import mx.controls.ToolTip;
	import mx.controls.Tree;
	import mx.core.Application;
	import mx.events.ListEvent;
	import mx.events.ResizeEvent;
	import mx.events.TreeEvent;
	import mx.managers.ToolTipManager;
	
	import nl.wv.extenders.panel.SuperPanel;
	
	import tools.*;
	
	public class Course
	{
		private var panel:SuperPanel = new SuperPanel();
		private var m:Tree = new Tree();
		private var newTopicBtn:Button = new Button();
		private var godine:ComboBox = new ComboBox();
		private var tool:Tool = new Tool();
		private var service:Mysql = new Mysql();
		private var service1:Mysql = new Mysql();
		private var service2:Mysql = new Mysql();
		private var service3:Mysql = new Mysql();
		private var service4:Mysql = new Mysql();
		private var service5:Mysql = new Mysql();
		private var x:XML;
		private var xx:Array = new Array();
		private var kurs:String;
		private var tip:ToolTip;
		private var createToolTip:Boolean = true;
		private var xmlPom:String = "";
		
		public function Course(p:SuperPanel){
			panel = p;
			m.x = 10;
			m.y = 34;
	 		m.height=panel.height-85;
	 		m.width=panel.width-30;
			newTopicBtn.label = "NOVA TEMA";
			newTopicBtn.x = panel.width-113;
			newTopicBtn.y = panel.height-45;
			newTopicBtn.addEventListener(MouseEvent.CLICK, onNewTopicBtnClick);
			godine.x = 10;
			godine.width = panel.width - 130;
			godine.y = panel.height-45;
			//godine.addEventListener(MouseEvent.CLICK, onNewTopicBtnClick);
			p.addChild(m);
			p.addChild(newTopicBtn);
			p.addChild(godine);
			panel.addEventListener(ResizeEvent.RESIZE, onResize);
		}
		
		public function onNewTopicBtnClick(e:MouseEvent):void{
			if(!panel.parentApplication.checkExist("NOVA TEMA")){
				var s:SuperPanel;
				panel.parentApplication.panels.push(s = tool.createPanel("NOVA TEMA", 0, 400, 400, 400, 100, true, true, "Materijal"));
				panel.parentApplication.addChild(s);
				s.backButton.addEventListener(MouseEvent.CLICK, panel.parentApplication.taskbarButtonClicked);
				s.backButton.addEventListener(MouseEvent.RIGHT_CLICK, panel.parentApplication.taskbarButtonRightClicked);
				var newTopic:NewTopic = new NewTopic(s);
				newTopic.setParameters(XML(m.dataProvider).root[0], panel.parentApplication.username, kurs, panel.title);
			}
		}
		
		public function setParameters(k:String):void{
			kurs=k;
		}
		
		public function setDataProvider(xml:XML):void{
			m.dataProvider = xml;
			m.showRoot = false;
			m.showDataTips = true;
			m.labelField = "@naziv";
			setIcons();
			m.dataTipField = "@naziv";
			m.selectedIndex = 0;
       		m.addEventListener(ListEvent.ITEM_DOUBLE_CLICK, onDoubleClick);
       		m.addEventListener(TreeEvent.ITEM_OPEN, onOpen);
	 		m.addEventListener(ListEvent.ITEM_ROLL_OVER, overList);
	 		m.addEventListener(ListEvent.ITEM_ROLL_OUT, outList);
			service2.sql = {type:"SELECT DISTINCT", a1:"g.godina", a2:"g.id_godina", from:"godina g, materijal m", where:"m.godina=g.id_godina"};
	 		service2.addEventListener(service2.MYSQL_RESULT, onGodineResult);
	 		service2.startService();		 		
		}
		
		public function onGodineResult(e:Event):void{
			var xmlList:XMLList = service2.xml.children();
			var xmlPom1:String = "<root naziv='Godine' tip='-1'>";
			for (var i:int=0; i<xmlList.length(); i++) {
				xmlPom1+="<god id='"+xmlList[i].id_godina+"' godina='"+xmlList[i].godina+"'> </god>";
			}
			xmlPom1+="</root>";
			godine.dataProvider = new XML(xmlPom1).children();
			godine.labelField="@godina";
			godine.addEventListener(ListEvent.CHANGE, onGodineClick);
		}
		
		public function onGodineClick(e:ListEvent):void{
			service3.sql = {type:"SELECT", a1:"r.naziv", a2:"r.opis", a3:"r.url", a4:"r.tip", a5:"r.vrijeme", a6:"p.ime", a7:"p.prezime", a8:"r.datum", a9:"m.id_materijal", a10:"m.kurs", a11:"m.attachments", from:"materijal m, predavac p, godina g, resursi r", where:"r.id_resurs=m.attachments AND m.predavac=p.id_predavac AND g.godina=@"+e.currentTarget.selectedItem.@godina+"@ AND g.id_godina=m.godina AND m.kurs="+kurs};
	 		service3.addEventListener(service3.MYSQL_RESULT, onCoursesResult);
	 		service3.startService();
		}
		
		private function onCoursesResult(e:Event):void{ 
			service3.removeEventListener(service3.MYSQL_RESULT, onCoursesResult);
			var xmlList:XMLList = service3.xml.children();
			xmlPom = "<tree><root naziv='Materijali' tip='-1'>";
			for (var i:int=0; i<xmlList.length(); i++) {
				xmlPom+="<materijal resurs='"+xmlList[i].attachments+"' kurs='"+xmlList[i].kurs+"' id='"+xmlList[i].id_materijal+"' naslov='"+xmlList[i].naziv+"' naziv='"+xmlList[i].naziv+"' opis='"+xmlList[i].opis+"' datum='"+xmlList[i].datum+"' attachments='"+xmlList[i].url+"' tip='"+xmlList[i].tip+"' vrijeme='"+xmlList[i].vrijeme+"' tekst='"+xmlList[i].url+"' ime='"+xmlList[i].ime+"' prezime='"+xmlList[i].prezime+"'><tema naziv='Ucitavanje...' /></materijal>";
			}
			xmlPom+="</root>";
			
			service4.sql = {type:"SELECT", a1:"t.id_tema", a2:"t.naslov", a3:"t.tekst", a4:"k.username", a5:"t.datum", a6:"t.vrijeme", a7:"count(c.id_komentar)", from:"tema t, korisnik k, kurs q, komentar c", where:"t.kurs=q.id_kurs AND k.id_username=t.username AND t.id_tema=c.tema AND q.id_kurs="+kurs+" AND t.materijal is null GROUP BY id_tema"+" ORDER BY t.datum DESC, t.vrijeme DESC"};
	 		service4.addEventListener(service4.MYSQL_RESULT, onThemes1Result);
	 		service4.startService();
		}
		
		private function onThemes1Result(e:Event):void{
			service4.removeEventListener(service4.MYSQL_RESULT, onThemes1Result);
			var xmlList:XMLList = service4.xml.children();
			xmlPom+="<root naziv='Teme sa odgovorima' tip='-1' >";
			for (var i:int=0; i<xmlList.length(); i++) {
				xmlPom+="<tema brojkomentara='"+xmlList[i].count_c_id_komentar_+"' tip='2' naziv='"+ (String(xmlList[i].naslov).length>25 ? String(xmlList[i].naslov).substring(0,25)+"..." : xmlList[i].naslov) + "' naslov='"+xmlList[i].naslov+"' id='"+xmlList[i].id_tema+"' tekst='"+xmlList[i].tekst+"' username='"+xmlList[i].username+"' datum='"+xmlList[i].datum+"' vrijeme='"+xmlList[i].vrijeme+"'/>";
			}
			xmlPom+="</root>";
			
			service5.sql = {type:"SELECT", a1:"t.id_tema", a2:"t.naslov", a3:"t.tekst", a4:"k.username", a5:"t.datum", a6:"t.vrijeme", from:"tema t, korisnik k, kurs q", where:"t.kurs=q.id_kurs AND k.id_username=t.username AND t.materijal is null AND q.id_kurs="+kurs+" AND t.id_tema not in (SELECT tema from komentar)"+" ORDER BY t.datum DESC, t.vrijeme DESC"};
	 		service5.addEventListener(service5.MYSQL_RESULT, onThemes2Result);
	 		service5.startService();
		}
		
		private function onThemes2Result(e:Event):void{
			service5.removeEventListener(service5.MYSQL_RESULT, onThemes2Result);
			var xmlList:XMLList = service5.xml.children();
			xmlPom+="<root naziv='Teme bez odgovora' tip='-1' >";
			for (var i:int; i<xmlList.length(); i++) {
				xmlPom+="<tema tip='2' naziv='" + (String(xmlList[i].naslov).length > 25 ? String(xmlList[i].naslov).substring(0,25)+"..." : xmlList[i].naslov )+"' naslov='"+xmlList[i].naslov+"' id='"+xmlList[i].id_tema+"' tekst='"+xmlList[i].tekst+"' username='"+xmlList[i].username+"' datum='"+xmlList[i].datum+"' vrijeme='"+xmlList[i].vrijeme+"'/>";
			}
			xmlPom+="</root></tree>";
			var xml:XML=new XML(xmlPom);
			trace(xmlPom);
			xmlPom="";
			m.dataProvider = xml;
		}

		public function overList(event:ListEvent):void{
			if(createToolTip){
	 		m.selectedIndex = event.rowIndex;
			var xmlList:XMLList = m.dataProvider.children();
			var tekst:String = ""; 
			if(m.selectedItem.@tip==0) tekst = 			"Naslov: "+m.selectedItem.@naslov+"\n\n"+
														"Opis:\n"+tool.PlainTekst(m.selectedItem.@opis).substring(0,255)+"..."+"\n\n"+
														"Postavio predavac: "+m.selectedItem.@ime+" "+m.selectedItem.@prezime+"\n"+
														"Tip: PDF dokument\n"+
														"Datum: "+m.selectedItem.@datum+"\n"+
														"Vrijeme: "+m.selectedItem.@vrijeme+"\n";
			if(m.selectedItem.@tip==1) tekst = 			"Naslov: "+m.selectedItem.@naslov+"\n\n"+
														"Opis:\n"+tool.PlainTekst(m.selectedItem.@opis).substring(0,255)+"..."+"\n\n"+
														"Postavio predavac: "+m.selectedItem.@ime+" "+m.selectedItem.@prezime+"\n"+
														"Tip: Web stranica\n"+
														"Datum: "+m.selectedItem.@datum+"\n"+
														"Vrijeme: "+m.selectedItem.@vrijeme+"\n";
			if(m.selectedItem.@tip==2) tekst = 			"Naslov: "+m.selectedItem.@naslov+"\n\n"+
														"Tekst:\n"+tool.PlainTekst(m.selectedItem.@tekst).substring(0,255)+"..."+"\n\n"+
														"Korisnicko ime: "+m.selectedItem.@username+"\n"+
														"Tip: Tema\n"+
														"Datum: "+m.selectedItem.@datum+"\n"+
														"Vrijeme: "+m.selectedItem.@vrijeme+"\n";
			if(m.selectedItem.@tip==3) tekst = 			"Naslov: "+m.selectedItem.@naslov+"\n\n"+
														"Opis:\n"+tool.PlainTekst(m.selectedItem.@opis).substring(0,255)+"..."+"\n\n"+
														"Postavio predavac: "+m.selectedItem.@ime+" "+m.selectedItem.@prezime+"\n"+
														"Tip: Tekst\n"+
														"Datum: "+m.selectedItem.@datum+"\n"+
														"Vrijeme: "+m.selectedItem.@vrijeme+"\n";
            if(tekst!=""){
            	if(panel.x+panel.width+280<panel.parentApplication.screen.width){
	             		tip = ToolTipManager.createToolTip(tekst, panel.x+panel.width-20, Application.application.parent.mouseY) as ToolTip;
             		} else tip = ToolTipManager.createToolTip(tekst, panel.x, Application.application.parent.mouseY) as ToolTip;
             		tip.setStyle("backgroundColor",0xFFCC00);
             		tip.width = 300;
            	createToolTip = false;
   			}             
   		}
		}

		public function outList(event:ListEvent):void{
			if(!createToolTip) ToolTipManager.destroyToolTip(tip);
			createToolTip = true;
		}
		
		public function onOpen(e:TreeEvent):void{
			x = e.item as XML;
			if(x.localName()=="materijal" && !xx[e.item.@id]){
				xx[e.item.@id] = true;
				service.sql = {type:"SELECT", a1:"t.id_tema", a2:"t.naslov", a3:"t.tekst", a4:"k.username", a5:"t.datum", a6:"t.vrijeme", from:"tema t, korisnik k, kurs q", where:"t.kurs=q.id_kurs AND k.id_username=t.username AND t.materijal="+e.item.@id+" AND q.id_kurs="+e.item.@kurs+" ORDER BY t.datum DESC, t.vrijeme DESC"};
	 			service.addEventListener(service.MYSQL_RESULT, onThemesResult);
	 			service.startService();	
			}
		}
		
		public function onThemesResult(e:Event):void{
			service.removeEventListener(service.MYSQL_RESULT, onThemesResult);
			var xmlList:XMLList = service.xml.children();
			var i:int=0;
			for (; i<xmlList.length(); i++) {
				if(i==0) x.replace(0, new XML("<tema tip='2' naziv='" + (String(xmlList[i].naslov).length > 25 ? String(xmlList[i].naslov).substring(0,25)+"..." : xmlList[i].naslov )+"' naslov='"+xmlList[i].naslov+"' id='"+xmlList[i].id_tema+"' tekst='"+xmlList[i].tekst+"' username='"+xmlList[i].username+"' datum='"+xmlList[i].datum+"' vrijeme='"+xmlList[i].vrijeme+"'/>"));
				 else x.appendChild(new XML("<tema tip='2' naziv='" + (String(xmlList[i].naslov).length > 25 ? String(xmlList[i].naslov).substring(0,25)+"..." : xmlList[i].naslov )+ "' naslov='"+xmlList[i].naslov+"' id='"+xmlList[i].id_tema+"' tekst='"+xmlList[i].tekst+"' username='"+xmlList[i].username+"' datum='"+xmlList[i].datum+"' vrijeme='"+xmlList[i].vrijeme+"'/>"));				
			} 
			if(i==0) x.replace(0, new XML("<tema tip='-1' naziv='Nema tema...' />"));
			setIcons();
		}

		public function onResize(e:ResizeEvent):void{
			m.height=panel.height-85;
	 		m.width=panel.width-30;
			newTopicBtn.x = panel.width-113;
			newTopicBtn.y = panel.height-45;
			godine.width = panel.width - 130;
			godine.y = panel.height-45;
		}
				
		public function onDoubleClick(e:ListEvent):void{			
			if(!panel.parentApplication.checkExist(m.selectedItem.@naslov)&&String(m.selectedItem.@naslov).length>0){
				if(m.selectedItem.@tip == 2){
					var s:SuperPanel;
					panel.parentApplication.panels.push(s = tool.createPanel(m.selectedItem.@naslov, 0, 400, 500, 300, 0, true, true, "Materijal"));
					panel.parentApplication.addChild(s);
					s.backButton.addEventListener(MouseEvent.CLICK, panel.parentApplication.taskbarButtonClicked);
					s.backButton.addEventListener(MouseEvent.RIGHT_CLICK, panel.parentApplication.taskbarButtonRightClicked);
					var topic:Topic = new Topic(s);
					s.height = 500;
					topic.show(m.selectedItem.@tekst, m.selectedItem.@naslov, m.selectedItem.@username, m.selectedItem.@datum, m.selectedItem.@vrijeme, m.selectedItem.@id);
				} else {
					service1.sql = {type:"SELECT", a1:"r.id_resurs", a2:"r.url", a3:"r.user", a4:"k.username", a5:"t.tip", a6:"t.program", a7:"r.sifra", a8:"r.naziv", a9:"r.opis", a10:"r.datum", a11:"r.vrijeme", from:"resursi r, korisnik k, tipovi t", where:"r.tip=t.id_tip AND r.user=k.id_username AND r.id_resurs="+m.selectedItem.@resurs };
	 				service1.addEventListener(service1.MYSQL_RESULT, onOpenResource);
	 				service1.startService();
	 			}
			}
		}
		
		public function onOpenResource(e:Event):void{
			var xmlList:XMLList = service1.xml.children();
			if(xmlList.length()>0){
				if(!panel.parentApplication.checkExist("Resurs("+xmlList.id_resurs+")")){
					var s:SuperPanel;
					panel.parentApplication.panels.push(s = tool.createPanel("Resurs("+xmlList.id_resurs+")", 0, 330, 280, 300, 150, true, false, "Resurs("+xmlList.id_resurs+")"));
					panel.parentApplication.addChild(s);
					s.backButton.addEventListener(MouseEvent.CLICK, panel.parentApplication.taskbarButtonClicked);
					s.backButton.addEventListener(MouseEvent.RIGHT_CLICK, panel.parentApplication.taskbarButtonRightClicked);
					var resurs:Resource = new Resource(s);
					resurs.show(xmlList.id_resurs, xmlList.url, xmlList.tip, xmlList.user, xmlList.username, xmlList.program, xmlList.sifra, xmlList.naziv, xmlList.opis, xmlList.datum, xmlList.vrijeme);
					if(xmlList.sifra!="") {
						resurs.sifrirano = true;				
					} else resurs.sifrirano = false;
				}
			} else tool.createAlert("Greska!", "Resurs sa ukucanim id-om ne postoji!");
		}
		
		public function setIcons():void{
			for(var i:Number = 0; i<m.dataProvider.length; i++){
				var xmlList:XMLList = m.dataProvider.getItemAt(i).children();
				for(var j:Number = 0; j<xmlList.length(); j++){
					var xmlList1:XMLList = m.dataProvider.getItemAt(i).root[j].children();
					for(var k:Number = 0; k<xmlList1.length(); k++){
						if(j==0) {
							if(m.dataProvider.getItemAt(i).root[j].materijal[k].@tip == 0) m.setItemIcon(m.dataProvider.getItemAt(i).root[j].materijal[k], panel.parentApplication.pdf, panel.parentApplication.pdf);
							if(m.dataProvider.getItemAt(i).root[j].materijal[k].@tip == 1) m.setItemIcon(m.dataProvider.getItemAt(i).root[j].materijal[k], panel.parentApplication.www, panel.parentApplication.www);
							if(m.dataProvider.getItemAt(i).root[j].materijal[k].@tip == 3) m.setItemIcon(m.dataProvider.getItemAt(i).root[j].materijal[k], panel.parentApplication.html, panel.parentApplication.html);
							var xmlList2:XMLList = m.dataProvider.getItemAt(i).root[j].materijal[k].children();
							for(var l:Number = 0; l<xmlList2.length(); l++){
								if(m.dataProvider.getItemAt(i).root[j].materijal[k].tema[l].@tip == 2) m.setItemIcon(m.dataProvider.getItemAt(i).root[j].materijal[k].tema[l], panel.parentApplication.theme, panel.parentApplication.theme);	
							}
						} else if(j==1||j==2) {
							if(m.dataProvider.getItemAt(i).root[j].tema[k].@tip == 2) m.setItemIcon(m.dataProvider.getItemAt(i).root[j].tema[k], panel.parentApplication.theme, panel.parentApplication.theme);							
						}
					}
				}
			}
		}
	}
}